/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package do_.com.bpd.business.referrals.persistence.controllers;

import do_.com.bpd.business.referrals.persistence.entities.BusinessReferralCase;
import do_.com.bpd.business.referrals.persistence.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import org.eclipse.persistence.internal.jpa.querydef.ExpressionImpl;
import org.eclipse.persistence.internal.jpa.querydef.OrderImpl;

/**
 *
 * @author Developer
 */
public class BusinessReferralCaseJpaController implements Serializable {

    public BusinessReferralCaseJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }

    public BusinessReferralCaseJpaController() {
        this.utx = null;
        this.emf = Persistence.createEntityManagerFactory("ReferralPersistencePU");
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(BusinessReferralCase businessReferralCase) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(businessReferralCase);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                try {
                    em.close();
                } catch (Exception ex) {
                    System.out.println("ERROR:" + ex.getMessage());
                }
            }
        }
    }

    public void edit(BusinessReferralCase businessReferralCase) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            businessReferralCase = em.merge(businessReferralCase);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = businessReferralCase.getId();
                if (findBusinessReferralCase(id) == null) {
                    throw new NonexistentEntityException("The businessReferralCase with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            BusinessReferralCase businessReferralCase;
            try {
                businessReferralCase = em.getReference(BusinessReferralCase.class, id);
                businessReferralCase.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The businessReferralCase with id " + id + " no longer exists.", enfe);
            }
            em.remove(businessReferralCase);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<BusinessReferralCase> findBusinessReferralCaseEntities() {
        return findBusinessReferralCaseEntities(true, -1, -1);
    }

    public List<BusinessReferralCase> findBusinessReferralCaseEntities(int maxResults, int firstResult) {
        return findBusinessReferralCaseEntities(false, maxResults, firstResult);
    }

    private List<BusinessReferralCase> findBusinessReferralCaseEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            CriteriaBuilder cb = em.getCriteriaBuilder();
            CriteriaQuery<BusinessReferralCase> q = cb.createQuery(BusinessReferralCase.class);
            Root<BusinessReferralCase> c = q.from(BusinessReferralCase.class);
            q.select(c);
            q.orderBy(cb.asc(c.get("timeStamp")),cb.asc(c.get("source")),cb.asc(c.get("id")));
            Query qw = em.createQuery(q);
            if (!all) {
                qw.setMaxResults(maxResults);
                qw.setFirstResult(firstResult);
            }
            return qw.getResultList();
        } finally {
            if(em!=null)
            em.close();
        }
    }

    public BusinessReferralCase findBusinessReferralCase(Integer id) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            return em.find(BusinessReferralCase.class, id);
        } finally {
            if(em!=null)
            em.close();
        }
    }

    public int getBusinessReferralCaseCount() {
        EntityManager em = null;
        try {
            em = getEntityManager();
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<BusinessReferralCase> rt = cq.from(BusinessReferralCase.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            if(em!=null)
            em.close();
        }
    }
        public void closeEntityManagerFactory() {
        if (emf != null) {
            emf.close();
        }
    }
}
